<?php

namespace app\app\controller;

use app\app\model\ProjectPhoneModel;

use app\BaseController;
use PhpOffice\PhpSpreadsheet\Spreadsheet;


class ProjectPhone extends BaseController
{
    public function index()
    {
        $data = $this->request->param();
        $pageSize = input('pageSize', '20');

        //筛选条件
        $where[] = ['state', '=', '正常'];
        if (isset($data['name'])) {
            $where[] = ['name', 'like', '%' . $data['name'] . '%'];
        }
        if (isset($data['department'])) {
            $where[] = ['department', 'like', '%' . $data['department'] . '%'];
        }
        if (isset($data['city'])) {
            $where[] = ['city', 'like', '%' . $data['city'] . '%'];
        }
        if (isset($data['company_name'])) {
            $where[] = ['company_name', 'like', '%' . $data['company_name'] . '%'];
        }
        if (isset($data['position'])) {
            $where[] = ['position', 'like', '%' . $data['position'] . '%'];
        }

        $list = ProjectPhoneModel::where($where)
            ->orderRaw("field(company_name,'省超高压公司') desc")
            ->paginate($pageSize);
        $this->success('success', $list);
    }

    //导入数据
    public function import()
    {
        // 获取表单上传文件
        $file[] = request()->file('file');
        try {
            // 验证文件大小，名称等是否正确
            validate(['file' => 'filesize:51200|fileExt:xls,xlsx'])
                ->check($file);
            $savename = \think\facade\Filesystem::putFile('topic', $file[0]);
            $fileExtendName = substr(strrchr($savename, '.'), 1);
            $spreadsheet = new Spreadsheet();
            // 有Xls和Xlsx格式两种
            if ($fileExtendName == 'xlsx') {
                $objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
            } else {
                $objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xls');
            }
            $objReader->setReadDataOnly(TRUE);
            // 读取文件，tp6默认上传的文件，在runtime的相应目录下，可根据实际情况自己更改
            $objPHPExcel = $objReader->load(root_path() . '/runtime/storage/' . $savename);
            $sheet = $objPHPExcel->getSheet(0);   //excel中的第一张sheet
            $highestRow = $sheet->getHighestRow();       // 取得总行数
            $highestColumn = $sheet->getHighestColumn();   // 取得总列数
            \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
            $lines = $highestRow - 1;
            if ($lines <= 0) {
                echo('数据不能为空！');
                exit();
            }
            $oldchar = array(" ", "　", "\t", "\n", "\r");
            $newchar = array("", "", "", "", "");
            //循环读取excel表格，整合成数组。如果是不指定key的二维，就用$data[i][j]表示。
            for ($j = 2; $j <= $highestRow; $j++) {
                $data[$j - 2] = [
                    'name' => str_replace($oldchar, $newchar, $objPHPExcel->getActiveSheet()->getCell("A" . $j)->getValue()),
                    'company_name' => str_replace($oldchar, $newchar, $objPHPExcel->getActiveSheet()->getCell("B" . $j)->getValue()),
                    'department' => str_replace($oldchar, $newchar, $objPHPExcel->getActiveSheet()->getCell("C" . $j)->getValue()),
                    'position' => str_replace($oldchar, $newchar, $objPHPExcel->getActiveSheet()->getCell("D" . $j)->getValue()),
                    'city' => str_replace($oldchar, $newchar, $objPHPExcel->getActiveSheet()->getCell("E" . $j)->getValue()),
                    'phone' => str_replace($oldchar, $newchar, $objPHPExcel->getActiveSheet()->getCell("F" . $j)->getValue()),
                    'in_tel' => str_replace($oldchar, $newchar, $objPHPExcel->getActiveSheet()->getCell("G" . $j)->getValue()),
                    'out_tel' => str_replace($oldchar, $newchar, $objPHPExcel->getActiveSheet()->getCell("H" . $j)->getValue()),
                ];
            }
            // 实例化model
            $model = new ProjectPhoneModel();
            // 批量添加数据
            if ($model->saveAll($data)) {
                return 200;
            }
            return 500;
        } catch (\think\exception\ValidateException $e) {
            return $e->getMessage();
        }
    }

    //导出数据
    public function export()
    {
        $spreadsheet = new Spreadsheet();
        //
    }


}
